// MegaComboBox.cpp : implementation file
//

#include "stdafx.h"
#include "MegaComboBox.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMegaComboBox
#define COLOR_SEL			RGB(255, 255, 255)
#define COLOR_TEXT_SEL		RGB(0, 0, 0)

CMegaComboBox::CMegaComboBox()
{
	m_pIcon = NULL;
}

CMegaComboBox::~CMegaComboBox()
{
	if(m_pIcon) 
		delete m_pIcon;
}

void CMegaComboBox::SetBitmap(UINT uiIcon)
{
	if(m_pIcon) 
		delete m_pIcon;

	m_pIcon = new CMegaBitmap();
	m_pIcon->LoadBitmap(uiIcon);
}

void CMegaComboBox::RecalcMinWidth()
{
	// set new minimal width of dropped list
	int dx = 0;
	CDC * pDC = GetDC();
	for (int i = 0; i < GetCount(); i++)
	{
		CString str;
		GetLBText(i, str);
		CSize sz = pDC->GetTextExtent(str);

		if(sz.cx > dx)
			dx = sz.cx;
	}
	ReleaseDC(pDC);

	// Adjust the width for the vertical scroll bar and the left and right border.
	dx += ::GetSystemMetrics(SM_CXVSCROLL) + 2*::GetSystemMetrics(SM_CXEDGE);

	// Set the width of the list box so that every item is completely visible.
	SetDroppedWidth(dx);
}

BEGIN_MESSAGE_MAP(CMegaComboBox, CComboBox)
	//{{AFX_MSG_MAP(CMegaComboBox)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMegaComboBox message handlers

void CMegaComboBox::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct) 
{
	if(!m_pIcon)
		return ;

	CRect rc;
	GetClientRect(&rc);

	CSize sz = m_pIcon->GetBitmapDimension();
	lpMeasureItemStruct->itemHeight = m_pIcon?sz.cy+4:rc.Height();
	lpMeasureItemStruct->itemWidth = rc.Width();
}

void CMegaComboBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
	if(((int) lpDrawItemStruct->itemID) < 0)
		return ;

	CRect rcItem(lpDrawItemStruct->rcItem);
	CSize sz = m_pIcon?m_pIcon->GetBitmapDimension():CSize(0, 0);

	CString str = "--";
	GetLBText(lpDrawItemStruct->itemID, str);

	CDC dc;
	dc.Attach(lpDrawItemStruct->hDC);

	// Save these value to restore them when done drawing.
	COLORREF crOldTextColor = dc.GetTextColor();
	COLORREF crOldBkColor = dc.GetBkColor();

	dc.SetTextColor(COLOR_TEXT_SEL);
	dc.SetBkColor(COLOR_SEL);
	dc.FillSolidRect(&rcItem, COLOR_SEL);

	if ((lpDrawItemStruct->itemAction | ODA_SELECT) &&
	(lpDrawItemStruct->itemState  & ODS_SELECTED))
	{
		dc.SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
		dc.SetBkColor(::GetSysColor(COLOR_HIGHLIGHT));
		dc.FillSolidRect(&lpDrawItemStruct->rcItem, ::GetSysColor(COLOR_HIGHLIGHT));
	}
	else
		dc.FillSolidRect(&lpDrawItemStruct->rcItem, crOldBkColor);

	/*
	if(	((lpDrawItemStruct->itemAction & ODA_FOCUS) && (lpDrawItemStruct->itemState & ODS_FOCUS)) ||
		((lpDrawItemStruct->itemAction & ODA_SELECT) && (lpDrawItemStruct->itemState & ODS_SELECTED)) ||
		(lpDrawItemStruct->itemState & ODS_COMBOBOXEDIT))
	{
		dc.DrawFocusRect(&rcItem);
	}
	*/

   	// draw icon
	if(m_pIcon)
	{
		m_pIcon->Draw(&dc, rcItem.left, rcItem.top+(rcItem.Height()-sz.cy)/2);
		rcItem.left += sz.cx+1;
	}
	rcItem.top++;
	rcItem.bottom--;

	// Draw the text.
	dc.DrawText(str, &rcItem, DT_LEFT|DT_SINGLELINE|DT_VCENTER);

	// Reset the background color and the text color back to their
	// original values.
	dc.SetTextColor(crOldTextColor);
	dc.SetBkColor(crOldBkColor);

	dc.Detach();
}

int CMegaComboBox::CompareItem(LPCOMPAREITEMSTRUCT lpCompareItemStruct) 
{
	// TODO: Add your code to determine the sorting order of the specified items
	// return -1 = item 1 sorts before item 2
	// return 0 = item 1 and item 2 sort the same
	// return 1 = item 1 sorts after item 2

	return -1;
}

